﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace e_TeachNext
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.SetFocus(txtUserIDLogin);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Request.Cookies["Login"] != null) {
                if (Request.Cookies["login"]["UserID"] == txtUserIDLogin.Text && Request.Cookies["Login"]["Password"] == txtPasswordLogin.Text)
                {
                    Application["UserID"] = txtUserIDLogin.Text;
                    Application["StudentID"] = Request.Cookies["Login"]["StudentNo"];
                    Response.Redirect("Course.aspx");
                }
                else {
                    lblStatus.Text = "Login Failed. Invalid user name and password. Do you want to register?";
                    mvwLogin.ActiveViewIndex = 2;
                }
            }
        }
        protected void lbtnRegister_Click(object sender, EventArgs e)
        {
            mvwLogin.ActiveViewIndex = 1;
            txtuserIDRegister.Focus();
        }
        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Application["UserID"] = txtuserIDRegister.Text;
            if (Application["StudentID"] != null)
            {
                Application.Lock();
                Application["StudentID"] = Convert.ToInt32(Application["StudentID"]) + 1;
                Application.UnLock();
            }
            else {
                Application["StudentID"] = 1;
            }

            Response.Cookies["Login"]["UserID"] = txtuserIDRegister.Text;
            Response.Cookies["Login"]["Password"] = txtPasswordRegister.Text;
            Response.Cookies["Login"]["StudentNo"] = Application["StudentID"].ToString();
            Response.Cookies["Login"].Expires = DateTime.MaxValue;
            Response.Redirect("Course.aspx");

        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtuserIDRegister.Text = "";
            txtPasswordRegister.Text = "";
            txtConfirmPasswordRegister.Text = "";
            txtuserIDRegister.Focus();
        }

        protected void btnCancelRegister_Click(object sender, EventArgs e)
        {
            mvwLogin.ActiveViewIndex = 0;
        }


    }
}